#ifndef FILE_PARSE_H_
#define FILE_PARSE_H_

#include <string>
#include <strstream>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <functional>
#include <algorithm>

#define PRECISION_HIGH

#ifdef PRECISION_HIGH
typedef double Real;
#else
typedef float Real;
#endif

const int FILE_PARSE_MFC = 0;
const int FILE_PARSE_STL = 1;
const int FILE_PARSE_MAP = 2;

//////////////////////////////////////////////////////////////////////////
// point2d
struct Point2D
{
	Real x,y;
	// construction
	Point2D() : x(0), y(0)	{}
	Point2D(const Point2D& p) : x(p.x), y(p.y)	{}
	Point2D(Real vx, Real vy) : x(vx), y(vy)	{}
	Point2D(int vx,int vy):x(Real(vx)),y(Real(vy)){}
	// operator
	Point2D operator+(const Point2D& p) const	{ return Point2D(x + p.x, y + p.y); }
	Point2D operator-(const Point2D& p) const	{ return Point2D(x - p.x, y - p.y); }

	// stream
	friend std::ostream& operator << ( std::ostream& out, const Point2D& rch )
	{
		out << rch.x << ' ' << rch.y << std::endl;
		return out;
	}
	friend std::istream& operator >> ( std::istream& in, Point2D& rch)
	{
		std::string linetext;
		std::getline(in,linetext);
		std::istrstream istrinf(linetext.c_str());
		istrinf >> rch.x >> rch.y;
		return in;
	}
	//////////////////////////////////////////////////////////////////////////
	// subclass
	class drawPoint2D
	{
	public:
		drawPoint2D(CDC& pdc):dc(pdc){};
		void operator()(const Point2D& point) const
		{
			CRect rect;
			rect.left = point.x - 2;
			rect.right = point.x + 2;
			rect.top = point.y - 2;
			rect.bottom = point.y + 2;
			dc.Ellipse(&rect);
		}
	protected:
		CDC& dc;
	};
};
typedef std::vector<Point2D> PointVect;
typedef std::vector<Point2D>::iterator IterPointVect;
typedef std::vector<Point2D>::const_iterator CIterPointVect;

//////////////////////////////////////////////////////////////////////////
// file parse
class CFileParse
{
public:
	virtual bool LoadData(PointVect& points,LPCSTR strFileName) = 0;
	virtual bool SaveData(PointVect& points,LPCSTR strFileName) = 0;
};

class CMFCFileParse: public CFileParse
{
public:
	virtual bool LoadData(PointVect& points,LPCSTR strFileName);
	virtual bool SaveData(PointVect& points,LPCSTR strFileName);
};

class CSTLFileParse: public CFileParse
{
public:
	virtual bool LoadData(PointVect& points,LPCSTR strFileName);
	virtual bool SaveData(PointVect& points,LPCSTR strFileName);
};

class CMAPFileParse: public CFileParse
{
public:
	virtual bool LoadData(PointVect& points,LPCSTR strFileName);
	virtual bool SaveData(PointVect& points,LPCSTR strFileName);
};
/////////////////////////////////////////////////////////////////////////
// my data
class CFPData
{
protected:
	PointVect vertexs;

public:
	// rand data creation
	void CreatRandData(int nPNum,CRect& rect);	
	// draw the vertexs
	void DrawVertexs(CDC* pDC);
	// load data
	bool LoadData(LPCSTR strFileName,int nloadStyle);
	bool SaveData(LPCSTR strFileName,int nloadStyle);
};

#endif